//package com.plian.system.service.hr.passport.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.plian.Tools.*;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.entity.hr.passport.PassportLend;
//import com.plian.system.entity.hr.passport.PassportManagement;
//import com.plian.system.entity.hr.passport.PassportReturn;
//import com.plian.system.mapper.hr.passport.PassportReturnMapper;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.service.hr.passport.IPassportLendService;
//import com.plian.system.service.hr.passport.IPassportManagementService;
//import com.plian.system.service.hr.passport.IPassportReturnService;
//import com.plian.system.service.wf.FormWorkflowService;
//import com.plian.system.vo.hr.passport.PassportReturnVO;
//import com.plian.system.wrapper.hr.passport.PassportReturnWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.*;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//
///**
// *  服务实现类
// *
// * @author gangan
// * @since 2019-12-10
// */
//@Slf4j
//@Service
//@AllArgsConstructor
//public class PassportReturnServiceImpl extends BaseServiceImpl<PassportReturnMapper, PassportReturn> implements IPassportReturnService, BaseFlowService {
//
//    private FormWorkflowService formWorkflowService;
//
//    private IPassportManagementService passportManagementService;
//
//    private IPassportLendService passportLendService;
//
//    private PassportReturnWrapper passportReturnWrapper;
//
//	@Override
//    @Transactional(rollbackFor=RuntimeException.class)
//    public boolean saveOrUpdate(PassportReturn passportReturn) {
//        if(passportReturn.getStatus()==null){
//            passportReturn.setStatus(FormStatusConstant.UNREVIEWED);
//        }
//
//        PassportLend passportLend = passportLendService.getById(passportReturn.getUpstreamCodeId());
//        passportLend.setDef1("被引用");
//        passportLendService.updateById(passportLend);
//
//        return super.saveOrUpdate(passportReturn);
//    }
//
//    @Override
//    public boolean deleteLogic(List<Long> ids) {
//    for (Long id : ids) {
//        PassportReturn passportReturn = getById(id);
//        if (passportReturn == null) {
//            return false;
//        } else if (passportReturn.getStatus() != null &&
//                (passportReturn.getStatus() != FormStatusConstant.STASH && passportReturn.getStatus() != FormStatusConstant.UNREVIEWED)) {
//            return false;
//        }
//        PassportLend passportLend = passportLendService.getById(passportReturn.getUpstreamCodeId());
//        passportLend.setDef1("未归还");
//        passportLendService.updateById(passportLend);
//        formWorkflowService.deleteProcessInstance(String.valueOf(id));
//    }
//    return super.deleteLogic(ids);
//    }
//
//    @Override
//    public HashMap<String,Object> commit(String id) {
//       handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//
//        //将需要提交的流程变量返回，用于工作流侧
//        return new HashMap<String,Object>();
//    }
//
//    @Override
//    public Boolean finish(String id) {
//        return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//    }
//
//    @Override
//    public Boolean reject(String id, int formState, String comment) {
//        return handleStatus(Long.valueOf(id), formState);
//    }
//
//    /**
//     * 统一处理状态机事件方法
//     * @param id 主键
//     * @param updateStatus 更新状态
//     * @return boolean
//     */
//    private Boolean handleStatus(Long id,int updateStatus) {
//        try {
//            PassportReturn passportReturn = getById(id);
//            /**
//             * 设置审批人和审批时间
//             */
//            if (updateStatus == FormStatusConstant.FINISH){
//                passportReturn.setApprovedUser(TokenUtil.getTokenUserId());
//                Date now = DateUtil.now();
//                passportReturn.setApprovedTime(now);
//
//                PassportLend passportLend = passportLendService.getById(passportReturn.getUpstreamCodeId());
//                passportLend.setDef1("已归还");
//                passportLendService.updateById(passportLend);
//
//                PassportManagement passportManagement = passportManagementService.getById(passportLend.getUpstreamCodeId());
//                passportManagement.setIsInLibrary(2);
//                passportManagementService.updateById(passportManagement);
//            }
//            passportReturn.setStatus(updateStatus);
//            updateById(passportReturn);
//            return true;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            return false;
//        }
//    }
//
//    @Override
//    public List<PageData> findByIds(List<String> formIds) {
//        LambdaQueryWrapper<PassportReturn> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.in(PassportReturn::getId, Func.toLongList(StringUtils.join(formIds, StringPool.COMMA)));
//        List<PassportReturn> passportReturns = list(queryWrapper);
//        List<PassportReturnVO> passportReturnVOS = passportReturnWrapper.entityToVO(passportReturns);
//        List<PageData> pageDataList = new ArrayList<>();
//        try{
//            if (CollectionUtil.isNotEmpty(passportReturnVOS)){
//                for (PassportReturnVO vo : passportReturnVOS){
//                    PageData pd = new PageData();
//                    pd.put("id", vo.getId());
//                    pd.put("UNITNAME", vo.getReturnerCompanyName());
//                    pd.put("APPLICANTNAME", vo.getCreateUserName());
//                    pd.put("applicantId", vo.getCreateUser());
//                    pd.put("applyOrganizationId", vo.getReturnerCompanyName());
//                    pd.put("formType", FormTypeConstant.HR_PASSPORT_RETURN_FORM);
//                    pd.put("orgName", vo.getReturnerCompanyName());
//                    pageDataList.add(pd);
//                }
//            }
//
//            return pageDataList;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//        }
//        return pageDataList;
//    }
//
//    @Override
//    public List<Integer> getFormType() {
//        return Arrays.asList(FormTypeConstant.HR_PASSPORT_RETURN_FORM);
//    }
//
//    @Override
//    public void goBackToStash(String formId) {
//        PassportReturn passportReturn = getById(Long.valueOf(formId));
//
//        PassportLend passportLend = passportLendService.getById(passportReturn.getUpstreamCodeId());
//        passportLend.setDef1("被引用");
//        passportLendService.updateById(passportLend);
//
//        PassportManagement passportManagement = passportManagementService.getById(passportLend.getUpstreamCodeId());
//        passportManagement.setIsInLibrary(1);
//        passportManagementService.updateById(passportManagement);
//
//        handleStatus(Long.valueOf(formId), UNREVIEWED);
//    }
//}
